Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 8000000UL
- #include <avr/io.h>
- #define KPAD_PORT PORTD
- #define KPAD_DDR DDRD
- #define KPAD_PIN PIND
- /*
- Column PD0 - PD3
- Rows PD4 - PD7
- _____
- PC6 | | pc5 (SCL)
- C0 - PD0 | | pc4 (SDA)
- C1 - PD1 | | pc3
- C2 - PD2 | | pc2 (ADC2)
- C3 - PD3 | | pc1 (ADC1)
- R0 - PD4 | | pc0 (ADC0)
- VCC | | GND
- GND | | AREF
- PB6 | | AVCC
- PB7 | | PB5 (SCK)
- R2 - PD5 | | PB4 (MISO)
- R3 - PD6 | | PB3 (MOSI)
- R4 - PD7 | | PB2 (SS)
- PB0 |_____| PB1
- Function return the key code when a key is pressed
- Keys are numbered as follows
- @param: None
- [01] [02] [03] [A]
- [04] [05] [06] [B]
- [07] [08] [09] [C]
- [*] [0] [#] [D]
- Return 255 if none of the keys are pressed.
- */
- uint8_t KeyPressed() {
- uint8_t row, colum;
- // The column C0 to C4 are kept at High Z State, these pins are not high or low, they are tristate. (inputs)
- // And their port value is set as low as well.
- // When the ddr (N)bit changes to 1, the port become output with value low.
- // 11110000
- // Enable pull-ups on the column's
- KPAD_PORT |= 0Xf0;
- for(colum=0; colum<4; colum++)
- {
- // Make all the pins inputs. (0)'s
- // The column inputs will have pullups as defined in KPAD_PORT
- KPAD_DDR &= ~(0Xff);
- _delay_ms(2);
- // Go though the colum's
- // Set the Column pin from High Z state to a output.
- // c=0 c=1 c=2 c=3
- // 1000nnnn, 0100nnnn, 0010nnnn, 0001nnnn
- KPAD_DDR |= (0X80>>colum);
- _delay_ms(2);
- for(row=0; row<4; row++)
- {
- // Go though each row.
- // r=0 r=1 r=2 r=3
- // nnnn1000, nnnn0100, nnnn0010, nnnn0001
- uint8_t pinvalue = KPAD_PIN & (0X08>>row);
- if(!(pinvalue))
- {
- // Return the button pressed!.
- return (row*4+colum);
- }
- }
- }
- return 0Xff;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement